/*
 * Copyright (C) 2005 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.swing.desktop.plugin.portunis.impl;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;

import nu.zoom.swing.ExtensionFilter;
import nu.zoom.swing.desktop.plugin.portunis.KeyStoreType;
import nu.zoom.swing.layout.VerticalPanel;

import org.ops4j.gaderian.Messages;

/**
 * A File chooser accessory that lets the user select the type of keystore to
 * open (PKCS12 or JKS).
 * 
 * @see javax.swing.JFileChooser#setAccessory(javax.swing.JComponent)
 * @author $Author: johan $
 * @version $Revision: 1.1 $
 */
public class KeyStoreFileChooserAccessory extends JComponent
{
	private ExtensionFilter jksFilter = new ExtensionFilter();
	private ExtensionFilter pkcs12Filter = new ExtensionFilter();
	private JPasswordField passwordField;
	private JRadioButton jksTypeButton;
	private JRadioButton pkcs12TypeButton;

	/**
	 * 
	 */
	public KeyStoreFileChooserAccessory(Messages messages,
			final JFileChooser parent) {
		super();
		jksFilter.addExtension(".jks");
		jksFilter.setDescription(messages
				.getMessage("keystore.load.filtype.jks"));
		pkcs12Filter.addExtension(".p12");
		pkcs12Filter.addExtension("pkcs12");
		pkcs12Filter.setDescription(messages
				.getMessage("keystore.load.filtype.pkcs12"));
		parent.setFileFilter(jksFilter);
		passwordField = new JPasswordField(20);
		passwordField.setToolTipText(messages
				.getMessage("keystore.load.password.tooltip"));

		ButtonGroup typeGroup = new ButtonGroup();
		jksTypeButton = new JRadioButton(messages
				.getMessage("keystore.load.filtype.jks"));
		jksTypeButton.setSelected(true);
		jksTypeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				parent.setFileFilter(jksFilter);
			}
		});
		typeGroup.add(jksTypeButton);
		pkcs12TypeButton = new JRadioButton(messages
				.getMessage("keystore.load.filtype.pkcs12"));
		pkcs12TypeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				parent.setFileFilter(pkcs12Filter);
			}
		});
		typeGroup.add(pkcs12TypeButton);
		VerticalPanel typePanel = new VerticalPanel();
		typePanel.addRow(jksTypeButton);
		typePanel.addRow(pkcs12TypeButton);
		typePanel.setBorder(BorderFactory.createTitledBorder(messages
				.getMessage("keystore.load.type")));

		JLabel passwordLabel = new JLabel(messages
				.getMessage("keystore.load.password"));
		VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.addRow(passwordLabel);
		mainPanel.addRow(passwordField);
		mainPanel.addRow(typePanel);
		setLayout(new BorderLayout());
		add(mainPanel, BorderLayout.CENTER);
		setPreferredSize(mainPanel.getPreferredSize());
	}

	public char[] getPassword()
	{
		return passwordField.getPassword();
	}

	public KeyStoreType getType()
	{
		if (jksTypeButton.isSelected()) {
			return KeyStoreType.JKS;
		} else {
			return KeyStoreType.PKCS12;
		}
	}
}
